package com.ruoyi.web.aspects;

import com.ruoyi.web.annotations.ApiController;
import com.ruoyi.core.entity.Output;
import com.ruoyi.core.exception.BaseException;
import com.ruoyi.web.exception.TokenException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wanghao 913351190@qq.com
 * @create 2021/6/24 14:14
 */
@RestControllerAdvice(annotations = {ApiController.class})
@Slf4j
@Order(1)
public class ApiExceptionHandler {

    /**
     * 处理global异常，目前返回状态码200
     */
    @ExceptionHandler(org.springframework.validation.BindException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public Object handleException(org.springframework.validation.BindException ex) {
        log.error("", ex);
        BindingResult bindingResult = ex.getBindingResult();
        if (bindingResult.getAllErrors() != null && bindingResult.getAllErrors().size() > 0) {
            List<String> messages = bindingResult.getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.toList());
            String message = String.join(",", messages);
            return failResult(message);
        }
        return failResult(ex.getMessage());
    }

    /**
     * 处理token验证异常，目前返回状态码401
     */
    @ExceptionHandler(TokenException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Object handleException(TokenException ex) {
        log.error("", ex);
        return failResult(ex.getCode(), ex.getMessage());
    }

    /**
     * 处理业务异常，目前返回状态码200
     */
    @ExceptionHandler(BaseException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public Object handleException(BaseException ex) {
        log.error("", ex);
        return failResult(ex.getCode(), ex.getMessage());
    }

//    /**
//     * org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error
//     */
//    @ExceptionHandler(org.springframework.http.converter.HttpMessageNotReadableException.class)
//    @ResponseBody
//    @ResponseStatus(HttpStatus.OK)
//    public Object handleException(org.springframework.http.converter.HttpMessageNotReadableException ex) {
//        return failResult("入参格式不正确");
//    }


    /**
     * 处理global异常，目前返回状态码200
     */
    @ExceptionHandler(Throwable.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public Object handleException(Throwable ex) {
        log.error("", ex);
        return failResult(ex.getMessage());
    }


    private Output<Object> failResult(Throwable ex) {
        return failResult(ex.getMessage());
    }

    private Output<Object> failResult(String message) {
        return Output.fail(message);
    }

    private Output<Object> failResult(Integer code, String message) {
        return Output.fail(code, message);
    }

}